<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id"
                :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="floor"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'showName',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/smis/floor/jfFloor/treeData"
                      :value="inputForm.floor.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.floor.id=value; $refs.selectPoint.showFloor(value); inputForm.x = ''; inputForm.y = ''; $refs.selectPoint.removeMarkPoint();}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="标注" prop="name"
                :rules="[
                 ]">
              <el-input v-model="inputForm.name" placeholder="请填写标注"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="uuid" prop="uuid"
                :rules="[
                  {required: true, message:'uuid不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.uuid" placeholder="请填写uuid"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="major" prop="major"
                :rules="[
                  {required: true, message:'major不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.major" placeholder="请填写major"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="mijor" prop="mijor"
                :rules="[
                  {required: true, message:'mijor不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.mijor" placeholder="请填写mijor"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
             <el-form-item label="设备高度" prop="z"
                 :rules="[
                   {required: true, message:'高度不能为空', trigger:'blur'}
                  ]">
               <el-input v-model="inputForm.z"  placeholder="UWB设备需要填写"     ></el-input>
            </el-form-item>
         </el-col>
       <el-col :span="12">
            <el-form-item label="x轴" prop="x"
                :rules="[
                  {required: true, message:'x轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.x" placeholder="请填写x轴"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="y轴" prop="y"
                :rules="[
                  {required: true, message:'y轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.y" placeholder="请填写y轴"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="23">
          <el-form-item label="单点定位" prop="singlePoint"
                        :rules="[
                 ]">
            <el-radio-group v-model="inputForm.singlePoint">
              <el-radio v-for="item in this.$dictUtils.getDictList('yes_no')" :label="item.value" :key="item.id">{{item.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="height: 400px;">
          <el-form-item label="所在位置" prop="x"
              :rules="[
                {required: true, message:'请选择所在位置', trigger:'blur'}
               ]">
            <select-point ref="selectPoint" type="ibeacon" @onMapClick="mapClick"></select-point>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          floor: {
            id: ''
          },
          name: '',
          uuid: '500A3E07-E190-4D1F-8CC3-3F76925346C8',
          major: '10194',
          mijor: '',
          x: '',
          y: '',
          z: '',
          singlePoint: '0'
        }
      }
    },
    components: {
      SelectTree,
      SelectPoint
    },
    methods: {
      init (method, id, floorId) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建ibeacon`
        } else if (method === 'edit') {
          this.title = '修改ibeacon'
        } else if (method === 'view') {
          this.title = '查看ibeacon'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/ibeacon/jfIbeacon/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfIbeacon)
              this.loading = false
              this.$refs.selectPoint.showFloorAndMarkPoint( this.inputForm.floor.id, this.inputForm.x, this.inputForm.y, this.inputForm.z || 0 )
            })
          } else if(floorId !== ''){
            this.inputForm.floor.id = floorId
            this.$refs.selectPoint.showFloor( floorId )
          }
          this.$refs.selectPoint.resize();
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/ibeacon/jfIbeacon/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
      }
    }
  }
</script>
